<template>
  <div class="login">
    <div class="loginbox">
      <!-- logo -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- form表单 -->
      <el-form ref="form" :model="form" :rules="rules" class="loginform">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-row class="btns">
          <el-button type="primary" plain @click="submitform('form')"
            >登录</el-button
          >
          <el-button type="info" plain @click="loginform('form')"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        // 用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        // 密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置
    loginform: function(form) {
      // console.log(this.$refs)
      // console.log(this.$refs[form])
      this.$refs[form].resetFields()
    },
    submitform: function(form) {
      // 登录
      this.$refs[form].validate(async valid => {
        // console.log(valid)
        // console.log(this.form)
        if (valid) {
          const ret = await this.$http.post('login', this.form)
          // console.log(ret)
          if (ret.data.meta.status === 200) {
            this.$message.success('登录成功')
            // window.sessionStorage
            // 1.生命周期为关闭浏览器窗口;
            // 2.在同一个窗口(页面)下数据可以共享;
            // 3.以键值对的形式存储使用;
            window.sessionStorage.setItem('token', ret.data.data.token)
            // console.log(ret)
            // console.log(ret.data.token)
            // 通过编程式导航跳转到后台主页，路由地址是 /home
            this.$router.push('/home')
          } else {
            this.$message.error('登录失败')
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100%;
}
.loginbox {
  position: absolute;
  width: 450px;
  height: 300px;
  border-radius: 5px;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .logo {
    position: relative;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      padding: 10px;
    }
  }
  .loginform {
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    .btns {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
